﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CaptureVision.Sunflower
{
    public class SunflowSection : ISceneFileTokenGenerator
    {
        protected const string SECTION_START_TAG        = "{";
        protected const string SECTION_END_TAG          = "}";
        protected const string SECTION_COMMENT_TAG      = "%";
        protected const string CRLF                     = "\r\n";
        protected const string TAB                      = "\t";
        protected const string SECTION_DEFAULT_NAME     = "DEFAULT_SECTION_NAME";


        /// <summary>
        /// Initializes a new instance of the <see cref="SunflowSection"/> class.
        /// </summary>
        public SunflowSection() : this(SECTION_DEFAULT_NAME)
        {}

        /// <summary>
        /// Initializes a new instance of the <see cref="SunflowSection"/> class.
        /// </summary>
        public SunflowSection(string sectionName) : this(sectionName, string.Empty)
        {}

        /// <summary>
        /// Initializes a new instance of the <see cref="SunflowSection"/> class.
        /// </summary>
        public SunflowSection(string sectionName, string description) : this(sectionName, description, string.Empty)
        {}

        /// <summary>
        /// Initializes a new instance of the <see cref="SunflowSection"/> class.
        /// </summary>
        public SunflowSection(string sectionName, string description, string comment)
        {
            SectionName = sectionName;
            Description = description;
            Comment     = comment;
            Properties  = new Dictionary<string, SunFlowProperty>();
        }


        /// <summary>
        /// Gets the scene file token string.
        /// </summary>
        /// <returns></returns>
        public string GetSceneFileTokenString()
        {
            var tokenBuilder = new StringBuilder();

            // Write the Section's opening tag
            tokenBuilder.AppendFormat("{0}{2}{1}{2}",
                                      SectionName,
                                      SECTION_START_TAG,
                                      CRLF);


            // write each section's properties..
            foreach (var property in Properties.Values)
            {
                tokenBuilder.AppendFormat("{0}{1}{2}",
                                          TAB,
                                          property.GetSceneFileTokenString(),
                                          CRLF);
            }


            // Write the Section's closing tag
            tokenBuilder.AppendFormat("{0}{1}",
                                      SECTION_END_TAG,
                                      CRLF);

            return tokenBuilder.ToString();
        }


        /// <summary>
        /// Gets the properties.
        /// </summary>
        public IDictionary<string, SunFlowProperty> Properties { get; private set; }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        public string SectionName { get; set; }

        /// <summary>
        /// Gets or sets the description.
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// Gets or sets the comment.
        /// </summary>
        public string Comment { get; set; }
    }
}
